package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: Zhuqi
 * @Date: 2021/11/30 16:31
 * @Description:
 */
@Service
public class BrandByChenServiceImpl implements BrandByChenService {
    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public BaseData list(BaseParam param, String id, String name) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();

        if (id != null&&!"".equals(id)) {

            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketBrand> users = brandMapper.selectByExample(example);


        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), 1);

    }

    @Override
    public MarketBrand brandCreate(MarketBrand brand) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();

        Date date = new Date();
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        int i = brandMapper.insertSelective(brand);
        criteria.andIdEqualTo(brand.getId());

        return brand;
    }

    @Override
    public MarketBrand brandUpdate(MarketBrand brand) {
        MarketBrandExample example = new MarketBrandExample();
        Date date = new Date();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        brand.setUpdateTime(date);
//      brand.setAddTime(date);
        brandMapper.updateByExampleSelective(brand, example);

        return brand;
    }

    @Override
    public MarketBrand brandDelete(MarketBrand brand) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        brand.setDeleted(true);
        criteria.andIdEqualTo(brand.getId());
        brandMapper.updateByExampleSelective(brand,example);
        return null;
    }
}
